package landx.toolkit.protocols 
{
	import flash.geom.Point;
	/**
	 * ...
	 * @author nease@163.com
	 */
	public class Coordinate
	{
		/**
		 * convert Stage Coordinate to Bevel
		 * @param x Coordinate x in stage
		 * @param y Coordinate y in stage
		 * @return the Point converted
		 **/
		public static function BevelToStage(x:Number, y:Number, ang:Number, isAngle:Boolean = false):Point {
			if (isAngle) {
				ang = AngleToRadian(ang);
			}
			var coord:Point=new Point();
			coord.x = (x - y) * Math.sin(ang);
			coord.y = (x + y) * Math.cos(ang);
			return coord.clone();
		}
		
		/**
		 * convert Bevel Coordinate to Stage
		 * @param x Coordinate x in Bevel
		 * @param y Coordinate y in Bevel
		 * @return the Point converted
		 **/
		public static function StageToBevel(x:Number, y:Number, ang:Number, isAngle:Boolean = false):Point {
			if (isAngle) {
				ang = AngleToRadian(ang);
			}
			var coord:Point=new Point();
			coord.x = (x / Math.sin(ang) + y / Math.cos(ang)) / 2;
			coord.y = ( -x / Math.sin(ang) + y / Math.cos(ang)) / 2;
			return coord.clone();
		}
		
		/**
		 * 				180
		 * 				|
		 * 				|
		 * 270----------------------90
		 * 				|
		 * 				|
		 * 				0
		 * @param	x
		 * @param	y
		 * @return
		 */
		public static function AngleByPoint(x:Number, y:Number):Number {
			return Math.atan2(x,y)*180/Math.PI;
		}
		
		/**
		 * Points center
		 * @param list All Points
		 * @return this center Point
		 **/
		public static function CenterInPoints(list:Array):Point{
			var coord:Point = new Point();
			try{
				for (var i:Number = 0; i < list.length;i++ ){
					coord.x+=(list[i] as Point).x/list.length;
					coord.y+=(list[i] as Point).y/list.length;
				}
			}catch (e:Error) {
				coord = null;
			}
			return coord;
		}
		
		/**
		 * the Point And zero Point rotation ang
		 * 				180
		 * 				|
		 * 				|
		 * 270----------------------90
		 * 				|
		 * 				|
		 * 				0
		 * @param x
		 * @param y
		 * @param ang the rotation angle
		 * @return the Point rotationed
		 */
		public static function VectorRotation(x:Number, y:Number, ang:Number,isAngle:Boolean=false):Point {
			if (isAngle) {
				ang = AngleToRadian(ang);
			}
			var r:Number = Math.sqrt(x * x + y * y);
			var angler:Number = Math.atan2(x, y);
			angler += ang;
			var coord:Point = new Point();
			coord.x = r * Math.sin(angler);
			coord.y = r * Math.cos(angler);
			return coord;
		}
		
		/**
		 * Angle To Radian
		 * @param	ang
		 * @return
		 */
		public static function AngleToRadian(ang:Number):Number {
			return ang * Math.PI / 180;
		}
		
		/**
		 * Radian To Angle
		 * @param	ang
		 * @return
		 */
		public static function RadianToAngle(ang:Number):Number {
			return ang * 180 / Math.PI;
		}
		
		/**
		 * (x,y)->angle->(x1,y1)
		 * @param	x	xfrom
		 * @param	y	yfrom
		 * @param	x1	xto
		 * @param	y1	yto
		 * @return Radian bewteen two points
		 */
		public static function AngleBetween(x:Number,y:Number,x1:Number,y1:Number):Number {
			var ang2:Number = Math.atan2(x1, y1);
			var ang:Number = Math.atan2(x, y);
			return ang2 - ang;
		}
		
		/**
		 * angle [0-2PI]
		 * @param	ang
		 * @param	isAngle
		 * @return angle from 0 to 2PI
		 */
		public static function AngleIn2PI(ang:Number):Number {
			ang = ang + Math.PI * 2 * 100;
			ang = ang % (Math.PI * 2);
			return ang;
		}
		
		/**
		 * angle [0-360]
		 * @param	ang
		 * @param	isAngle
		 * @return angle from 0 to 360
		 */
		public static function AngleIn360(ang:Number):Number {
			ang = ang + 180 * 2 * 100;
			ang = ang % (180 * 2);
			return ang;
		}
	}
}